package com.beamfield.controller.api.mb;

import com.alibaba.fastjson.JSONObject;
import com.beamfield.common.ApiResult;
import com.beamfield.controller.common.MbBaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.service.manage.material.DemandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Zephyr
 * @Description:
 * @Date 2018/10/23
 */
@Controller
@RequestMapping(value = "/mb_api/demand")
public class MbDemandController extends MbBaseController {

    private final DemandService demandService;

    @Autowired
    public MbDemandController(DemandService demandService) {
        this.demandService = demandService;
    }

    /**
     * 总工审核
     * @param request
     * @return
     */
    @RequestMapping(value = "/verify", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult verify(HttpServletRequest request){
        JSONObject body = super.getBody(request);
        if(body == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        String code = body.getString("code");
        String appr = body.getString("appr");
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(appr)){
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
        Long userId = super.getUserId(request);
        try{
            return demandService.updateApprove(appr, code, userId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

}
